package com.ctg.payroll2.action.workflow;

import java.sql.SQLException;
import java.util.List;

import com.ctg.payroll2.action.BaseAction;
import com.ctg.payroll2.bean.filter.WorkflowTaskFilter;
import com.ctg.payroll2.bean.po.Role;
import com.ctg.payroll2.bean.po.workflow.FlowRunnext;
import com.ctg.payroll2.bean.po.workflow.FlowStepRun;
import com.ctg.payroll2.service.workflow.WorkflowTaskService;
import com.ctg.payroll2.util.Constants;
import com.ctg.platform.util.Page;
import com.ctg.platform.util.Parse;

public class WorkflowTaskAction extends BaseAction {
	private static final long serialVersionUID = -4383047290245089481L;
	/**
	 * 查询调价过滤类
	 */
	private WorkflowTaskFilter filter;
	private int flag;
	private FlowRunnext flowRunnext;
	private FlowStepRun flowStepRun;
	private WorkflowTaskService service;
	/**
	 * 翻页结果
	 */
	private Page result;
	private List resultL;
	private Integer isOk;
	

	public Integer getIsOk() {
		return isOk;
	}

	public void setIsOk(Integer isOk) {
		this.isOk = isOk;
	}

	@Override
	protected void commonOperations() {
		// TODO Auto-generated method stub

	}
	/**
	 * 薪资发放记录
	 * @return
	 * @throws SQLException 
	 */
	private int queryPayLogByPeriod() throws SQLException{
		filter.setCompany_code(currentUser.getCompanyCode());
		return service.queryPayLogByPeriod(filter);
		
	}
	/**
	 * 薪资结果表内所有当前公司下属员工有记录
	 * @return
	 * @throws SQLException 
	 */
	private int getEmployeeCountOfPayData() throws SQLException{
		filter.setCompany_code(currentUser.getCompanyCode());
		return service.getEmployeeCountOfPayData(filter);
		
	}
	/**
	 * 员工数
	 * @return
	 * @throws SQLException 
	 */
	private int getEmployeeCountOfCompany() throws SQLException{
		filter.setCompany_code(currentUser.getCompanyCode());
		return service.getEmployeeCountOfCompany(filter);
		
	}
	/**
	 * 发起审批
	 * @return
	 * @throws SQLException 
	 */
	private void addTask() throws SQLException{
		if(queryPayLogByPeriod()<=0){
			int paydatas=getEmployeeCountOfPayData();
			int emps=getEmployeeCountOfCompany();
			if(paydatas!=emps){//应为！=
				isOk=1;
			}else{
				if(service.queryFlowInstance(filter)==null){
					isOk=3;
					service.addTask(filter);
				}else{
					isOk=2;
				}
			}
		}else{
			isOk=0;
		}
	}
	@Override
	protected void createEntity() {
		filter.setCompany_code(currentUser.getCompanyCode());
		filter.setAccount_id(currentUser.getUserId());
		try {
			switch (flag) {
			case 1://发起审核
				   //验证1.薪资发放记录companycode->priod->pay_log
				   //验证2.薪资结果表内所有当前公司下属员工有记录companycode->priod->pay_data->employee_id和companycode->employee_id->pb_employee比较
				//通过则1.insert flow_insid(companycode->orgid->flow_orgid->flowid从flow_orgchart->flow_master->flow_step)复制第一步
				///////2.insert flow_runnext+执行人==
				///////3.update flow_runnext+执行人==状态->E
				///////4.insert 下一步的flow_runnext+执行人==
				addTask();
				break;
			case 2:
				break;
			case 3:
				break;
			case 4:
				break;
			case 5:
				break;
			default:
				break;
			}
		}  catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void deleteEntity() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void exportXLS() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void queryEntity() {
		if (filter == null) {
			filter = new WorkflowTaskFilter();
		}
		filter.setAccount_id(currentUser.getUserId());
		try {
			switch (flag) {
			case 1://Task的翻页查询
				result = getTasksByFilter();
				break;
			case 2://当前用户是否在起始步骤
				isOk = queryStartFlowStepRun();
				break;
			case 3://是否叶子节点
				isOk = querySubCompanyCount();
				break;
			case 4://下属公司
				resultL=getSubCompanys();
				break;
			case 5:
				resultL=service.queryCompanyPayData(filter);
				break;
			case 6:
				resultL=service.queryAreaFromPayData(filter);
				break;
			case 7:
				resultL=service.queryGroupFromPayData(filter);
				break;
			default:
				break;
			}
		}  catch (SQLException e) {
			e.printStackTrace();
		}
		
	}	
	/**
	 * 下属公司combobox
	 * @return
	 * @throws SQLException 
	 */
	private List getSubCompanys() throws SQLException{
		filter.setCompany_code(currentUser.getCompanyCode());
		return service.querySubCompanys(filter);
		
	}
	/**
	 * Task翻页查询
	 * @return
	 */
	private Page getTasksByFilter(){
		filter.setStartIndex(Parse.toInteger(request.getParameter("start") , 0));
		filter.setEndIndex(Parse.toInteger(request.getParameter("limit"), Constants.DEFAULT_PAGE_SIZE));
		if(filter.getCompany_code().equals("")||filter.getCompany_code().equals(null)){
			filter.setCompany_code(currentUser.getCompanyCode());
		}
		result = service.queryTask(filter);
		return result;
	}
	/**
	 * 是否叶子节点
	 * @return
	 * @throws SQLException 
	 */
	private Integer querySubCompanyCount() throws SQLException{
		filter.setCompany_code(currentUser.getCompanyCode());
		return service.querySubCompanyCount(filter);
		
	}
	/**
	 * 是否是审核账号
	 * @return
	 * @throws SQLException 
	 */
	private Integer queryStartFlowStepRun() throws SQLException{
		filter.setCompany_code(currentUser.getCompanyCode());
		flowStepRun=service.queryStartFlowStepRun(filter);
		if(flowStepRun.getRun_type().equals(Long.parseLong("1"))){
			currentUser.getUserId().equalsIgnoreCase(flowStepRun.getRun_user());
			return 1;
		}else if(flowStepRun.getRun_type().equals(Long.parseLong("0"))){
			List roles=currentUser.getRoleList();
			Role role;
			for(int i=0;i<roles.size();i++){
				role=(Role)roles.get(i);
				if(role.getRoleId().equalsIgnoreCase(flowStepRun.getRun_user())){
					return 1;
				}
			}
			return 0;
		}else{
			return 0;
		}
		
	}
	@Override
	protected void retrieveEntity() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void updateEntity() {
		filter.setAccount_id(currentUser.getUserId());
		try {
			switch (flag) {
			case 1:
				if(filter.getCompany_code().equals("")||filter.getCompany_code().equals(null)){
					filter.setCompany_code(currentUser.getCompanyCode());
				}
				service.updateTask(filter);
				isOk=3;
				break;
			case 2:
				if(filter.getCompany_code().equals("")||filter.getCompany_code().equals(null)){
					filter.setCompany_code(currentUser.getCompanyCode());
				}
				service.rollBackTask(filter);
				isOk=3;
				break;
			case 3:
				break;
			case 4:
				break;
			case 5:
				break;
			default:
				break;
			}
		}  catch (SQLException e) {
			e.printStackTrace();
		}
	}


	public WorkflowTaskFilter getFilter() {
		return filter;
	}

	public void setFilter(WorkflowTaskFilter filter) {
		this.filter = filter;
	}

	/**
	 * @return 返回result
	 */
	public Page getResult() {
		return result;
	}

	/**
	 * @param result 设置result
	 */
	public void setResult(Page result) {
		this.result = result;
	}

	public int getFlag() {
		return flag;
	}

	public void setFlag(int flag) {
		this.flag = flag;
	}


	/**
	 * @return 返回flowRunnext
	 */
	public FlowRunnext getFlowRunnext() {
		return flowRunnext;
	}

	/**
	 * @param flowRunnext 设置flowRunnext
	 */
	public void setFlowRunnext(FlowRunnext flowRunnext) {
		this.flowRunnext = flowRunnext;
	}

	public void setService(WorkflowTaskService service) {
		this.service = service;
	}

	public List getResultL() {
		return resultL;
	}

	public void setResultL(List resultL) {
		this.resultL = resultL;
	}

	public FlowStepRun getFlowStepRun() {
		return flowStepRun;
	}

	public void setFlowStepRun(FlowStepRun flowStepRun) {
		this.flowStepRun = flowStepRun;
	}

}
